<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>垃圾去向管理</title>
	<script type="text/javascript" src="js/jquery-3.5.1.min.js"></script>
	<script type="text/javascript" src="js/adminJScript.js"></script>
    <link rel="stylesheet" type="text/css" href="layui/css/layui.css" />
	<link rel="stylesheet" href="./css/main.css"/>
	<link rel="stylesheet" href="https://at.alicdn.com/t/font_2150071_oqmbug6i2f.css"/>
  </head>
  <style>
	 body{
		 width:100%;
		 min-width: 900px;
		 height:100%
	 }
	 .searchBar{
	 	width:99%;
	 	margin: 0 auto;
		/* border: 2px solid #000000; */
	 }
	 .demoTable{
		 /* border: 1px solid #0000FF; */
		 height:100%;
		 width: 100%;
	 }
	 .container{
/* 	   width: 450px;
	   height: 450px; */
	   margin: 10px;
	   padding: 20px;
	   border-radius: 8px;
	   box-shadow: 0 3px 18px rgba(28, 19, 19, 0.5);
	   font-size: 16px;
	 }
	 .layui-form-item{
		 margin: 15px;
	 }
  </style>
  <body>
	  <!-- 搜索开始-->
	  <div class="searchBar">
		<form class="layui-form" action="">
			  <div class="layui-form-item">
		          <div class="layui-input-inline">
		            <select name="type">
		              <option value="" selected="">选择垃圾类型</option>
		              <option value="1" >厨余垃圾</option>
		              <option value="2">有害垃圾</option>
		              <option value="3">可回收垃圾</option>
					  <option value="4">不可回收垃圾</option>
		            </select>
		          </div>
		          <div class="layui-input-inline">
		            <select name="status">
		              <option value="" selected="">请选择审核状态</option>
		              <option value="0">申请中</option>
		              <option value="1">申请成功</option>
		              <option value="2">申请失败</option>
		              <option value="3">已出库</option>
		            </select>
		          </div>
				  <div class="layui-input-inline" style="width: 100px;">
				    <input type="text"  class="layui-input" name="entryTime" id="entryTime" placeholder="出库时间">
				  </div>
				   <div class="layui-input-inline">
				        <button type="submit" class="layui-btn" lay-submit="" lay-filter="search">搜索</button>
				        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
				   </div>
				</div>  
		</form>
	  </div>
	  <!--搜索结束 -->
	  <!-- 数据表格开始-->
	 <div class="demoTable">
		  <table class="layui-hide" id="user" lay-filter="user"></table> 
	</div>
	<div id="detailTab" style="display: none">
		<table class="layui-hide" id="detail" lay-filter="detail"></table>
	</div>
	<!--数据表格结束  -->
   </body>
<!-- script开始 -->
	<!-- 行工具栏 -->
	<script type="text/html" id="barDemo">
		<button class="layui-btn layui-btn-xs" lay-event="pass">通过申请</button>
		<button class="layui-btn layui-btn-xs" lay-event="refuse">拒绝申请</button>
	</script>
	<!-- layui开始 -->
	<script src="layui/layui.js" charset="utf-8"></script>
	<script type="text/javascript">
	layui.use(['table','layer','form','laydate'], function(){
			var table = layui.table;
			var layer = layui.layer;
			var form = layui.form;
			var laydate=layui.laydate;
			var search;
			
	laydate.render({
			  elem: '#entryTime'
			});		
  //方法级渲染
  var tableIns=table.render({
    elem: '#user'
   ,url:'/transport/list'
   ,method:'post'
   ,contentType: 'application/json'
   ,where:search
   ,height:'full-80'
   ,cellMinwidth:'70'
	,toolbar: 'true' //开启头部工具栏，并为其绑定左侧模板
    ,cols: [[
      {field:'id', title: 'ID',width:'8%',sort: true, fixed: true,align:'center'}
      , {field: 'type', title: '垃圾运输列表', width: '14%',align:'center',event:'viewDetail', style:'cursor: pointer;background-color: #eee;',
			  templet: function (d) {
				  switch (d.type) {
					  case 1:
						  return '厨余垃圾';
					  case 2:
						  return "有害垃圾";
					  case 3:
						  return "可回收垃圾";
					  case 4:
						  return "不可回收垃圾";
				  }
			  }
		}
      ,{field:'weight', title: '总重量/kg',width:'13%',sort: true,align:'center'}
      ,{field:'status', title: '申请状态',width:'13%',align:'center'
	  ,templet:function(d){
		  switch(d.status){
			  case 0:
			  return '<span style="color: #40AFFE;">申请中</span>';
			  case 1:
			  return '<span style="color: #3700ff;">申请成功</span>';
			  case 2:
			  return '<span style="color: #ff0066;">申请失败</span>';
			  case 3:
			  return '<span style="color: #ff8400;">已出库</span>';
		  }
	  }}
      ,{field:'dest', title: '运出地址',width:'20%',align:'center'}
	  ,{field:'outTime', title: '出库时间',width:'13%',align:'center'}
	  ,{fixed: 'right', title:'操作', width:'170',align:'center'
		,templet:function (d) {
	  			if(d.status == 0){
	  				return '<button class="layui-btn layui-btn-xs" lay-event="pass">通过申请</button>'+
					'<button class="layui-btn layui-btn-xs" lay-event="refuse">拒绝申请</button>'
				}else{
	  				return '<button class="layui-btn layui-btn-xs layui-btn-disabled" disabled="true" lay-event="pass">通过申请</button>'+
	  				'<button class="layui-btn layui-btn-xs layui-btn-disabled" disabled="true" lay-event="refuse">拒绝申请</button>'
				}
			  }
    }
    ]]
	,page: true
	  , parseData: function(res) { //res 即为原始返回的数据
		  return {
			  "code": res.code, //解析接口状态
			  "msg": res.msg, //解析提示文本
			  "count": res.data.total, //解析数据长度
			  "data": res.data.records //解析数据列表
		  };
	  }
	  , response: {
		  statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
	  }
	  ,request: {
    	pageName: "displayStart",
		  limitName: "displayLength"
	  }
	,done:function(res,curr,count){
		console.log(res);
	}
  });
  /* 详情表格渲染*/
	table.render({
		elem:'#detail'
		,url:''
		,cols: [[
			{field:'id', title: 'ID', fixed: true,width:80,}
			,{field:'type', title: '垃圾类型',width:150,
				templet:function(d){
					switch(d.type){
						case 1: return "厨余垃圾";
						case 2: return "有害垃圾";
						case 3:return "可回收垃圾";
						case 4: return "不可回收垃圾";
					}}
			}
			,{field:'weight', title: '重量',width:80,}
			,{field:'origin', title: '来源',width:110,}
			,{field:'createTime',width:170, title: '入库时间'}]]
		,response: {
			statusCode: 200
		}
		, parseData: function(res) { //res 即为原始返回的数据
			return {
				"code": res.code, //解析接口状态
				"msg": res.msg, //解析提示文本
				"count": res.data.total, //解析数据长度
				"data": res.data.records //解析数据列表
			};
		}
	});
	
	var url;//添加或修改的url;
	var mainIndex;//关闭的弹出层索引
  //打开申请页面
  function openTransApply(){
  	  mainIndex=layer.open({
  	    type: 2, 
  	    title:"申请需要打包运出的垃圾",
  	    content: 'garbageSelect.html',/* '传入任意的文本或html' *///这里content是一个普通的String
		area:['800px','600px'],
		/* moveOut:true, */
/* 		area:'auto', */
/* 		move:false, */
	/* 	shadeClose:true, */
		anim:5,
		resize:false,
/* 		scrollbar: false, */
		success:function(index){
			url="";
		}
  	  });
  }

  /*添加或修改垃圾表单提交 */
  form.on('submit(addOrUpGarb)', function(data){
         //序列化表单数据
		  var params= $("#adOrUpForm").serialize();
		  layer.alert(params, {
            title: '最终的提交信息'
          })
          $.ajax({
            url:url,
            method:'POST',
            contentType: 'application/json',
            async:false,
            data:JSON.stringify(data.field),
            success: function(res) {
  				layer.alert(res.msg, {
  					end: function(){
  							if (res.code == 200) console.log("success");
            	         }
            	    })
					layer.close(mainIndex);
					tableIns.reload();
            	}
            })
            return false;
        });

/*搜索表单提交 */
  form.on('submit(search)', function(data){
		table.reload('user',{
			where: data.field,
			page:{
				curr :1,
			},
		})
	  return false;
        });


 //头工具栏事件
  table.on('toolbar(user)', function(obj){
	var checkStatus=table.checkStatus(obj.config.id);
	var data=checkStatus.data;
    switch(obj.event){
      case 'add':
		openTransApply(); 
		break;
      /* //自定义头工具栏右侧图标 - 提示
      case 'LAYTABLE_TIPS':
        layer.alert('这是工具栏右侧自定义的一个图标按钮');
      break; */
    };
  })

//监听行工具事件
  table.on('tool(user)', function(obj){
	var data = obj.data; //获得当前行数据
	var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
	var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
    if(layEvent === 'refuse'){
		layer.confirm("您确定拒绝申请吗？",function(){
				  $.ajax({
					  url: '/transport/passOrRefuse/false/'+ obj.data.id
					  ,type:'GET'
					  , success:function (data) {
						  layer.closeAll('loading');
						  if(data.code==200){
							  layer.msg('已拒绝！', {icon: 1,time:2000,shade:0.2});
							  location.reload(true);
						  }else{
							  layer.msg('拒绝失败！', {icon: 2,time:3000,shade:0.2});
						  }
					  }
				  })
		})
    }else if(layEvent==="pass"){
		layer.confirm("您确定同意申请吗？",function(){
				  $.ajax({
					  url: '/transport/passOrRefuse/true/'+ obj.data.id
					  ,type:'GET'
					  , success:function (data) {
						  layer.closeAll('loading');
						  if(data.code==200){
							  layer.msg('已批准！', {icon: 1,time:2000,shade:0.2});
							  location.reload(true);
						  }else{
							  layer.msg('未能批准！', {icon: 2,time:3000,shade:0.2});
						  }
					  }
				  })
		})
	}else if(layEvent === 'viewDetail'){
		table.reload('detail',{
			elem:'#detail'
			,url:'/garbage/getByTransportId/'+obj.data.id
		});
		layer.open({
			title:'运输列表详情',
			type:1,
			skin:'layui-layer-rim',
			area:['600px','400px'],
			content:$('#detailTab'),
		});
	}
  })

});
</script>
</html>